<?php
//$Id$
/*-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-*\
      author:   altair penaredondo
        date:   September 2, 2005
		
 description:   a simple class for displaying a page by number of records
 
       usage:   $pager  = Pager::getPagerData("numrow", "limit per page", "current page", "link");
			  	$offset = $pager->offset; 
				$limit  = $pager->limit;
				$link   = $pager->showPage;
\*-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-*/
class Pager
{
       function getPagerData($numHits, $limit=50, $page='1', $action, $option=true)
       {
           $numHits  = (int) $numHits;
           $limit    = max((int) $limit, 1);
           $page     = ($page) ? (int) $page : "1";
           $numPages = ceil($numHits / $limit);
		   $showPage = "<div align=center>"; //#0C79B4 blue option
		   
		   $showPage .= ($page > 1) ? "<a href=?module=".DEFAULT_MODULE."&action=$action&Page=" . ($page-1) . ">Prev</a>   [  " :
		   		        "<a>Prev</a>   [  ";
		   $showPage .= ($option) ? "<select onchange='javascript: window.location=\"?module=".DEFAULT_MODULE."&action=$action&$action&Page=\"+this.value'>":"";
		   for ($i=1; $numPages >= $i; $i++){

				if ($option==true){				   
				 $showPage .= ($i == $page) ? "<option selected >$i</option>" : "<option>$i</option>";
				}else{
				 $showPage .= ($i == $page) ? "     <a>$i</a>     " :
				              "     <a href=?module=".DEFAULT_MODULE."&action=$action&Page=$i>$i</a>     ";
				}
		   }
		   $showPage .= ($option) ? "</select>" : "";
		   $showPage .= ($page >= $numPages) ? "  ]   <a>Next</a></div>" :
				          "  ]   <a href=?module=".DEFAULT_MODULE."&action=$action&Page=" . ($page+1) . ">Next</a></div>";
		   
           $page = max($page, 1);
           $page = min($page, $numPages);

           $page = ($page < 1) ? 1 : $page;
           $offset = ($page - 1) * $limit;

           $ret = new stdClass;

           $ret->offset   = $offset;
           $ret->limit    = $limit;
           $ret->numPages = $numPages;
           $ret->page     = $page;
	       $ret->showPage = $showPage;

           return $ret;
       }
}
?>
